package com.sample.springbatch.job.sample.config;

import com.sample.springbatch.job.config.BaseConfig;
import com.sample.springbatch.job.exception.MySkipException;
import com.sample.springbatch.job.sample.listener.RepayOrderStepListener;
import com.sample.springbatch.job.sample.processor.HbBillLoanDetailProcessor;
import com.sample.springbatch.job.sample.processor.RepayOrderProcessor;
import com.sample.springbatch.job.sample.reader.HbBillLoanDetailFlatFileItemReader;
import com.sample.springbatch.job.sample.reader.RepayOrderFlatFileItemReader;
import com.sample.springbatch.job.sample.reader.RepayOrderJpaPagingItemReader;
import com.sample.springbatch.job.sample.reader.RepayOrderMultiResourceFlatFileItemReader;
import com.sample.springbatch.job.sample.writer.*;
import com.sample.springbatch.model.RepayOrderInfo;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.file.FlatFileParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;


/**
 * Created by zwg on 2017/7/14.
 */
@Configuration
public class HbBillLoanDetailConfig extends BaseConfig {



    @Autowired
    HbBillLoanDetailFlatFileItemReader hbBillLoanDetailFlatFileItemReader;
    @Autowired
    HbBillLoanDetailProcessor hbBillLoanDetailProcessor;
    @Autowired
    HbBillLoanDetailJpaItemWriter hbBillLoanDetailJpaItemWriter;


    // 注解@JobScope必须添加，才能在reader 中获取到参数
    @Bean(name = "hbBillLoanDetailJob")
    @JobScope
    public Job hbBillLoanDetailJob() {
        return jobBuilderFactory.get("hbBillLoanDetailJob")
                .incrementer(new RunIdIncrementer())
                .listener(myJobListener) //增加对job的监听
                .flow(hbBillLoanDetailStep())
                .end()
                .build();
    }

    @Bean(name="hbBillLoanDetailStep")
    public Step hbBillLoanDetailStep() {
        return stepBuilderFactory.get("hbBillLoanDetailStep")
                //.listener()  //添加对step的监听，可以实现多step的所有监听
                .<RepayOrderInfo, RepayOrderInfo> chunk(500)   //这些配置参数需要单独按job配置到一个公共的数据类中，支持通过application.yml 调整修改
                //跳过机制是针对每个执行线程设置，最大跳过数量为：skipLimit * throttleLimit
                .faultTolerant().skipLimit(0)
                .skip(FlatFileParseException.class)  //reader
                .skip(MySkipException.class) //processor 自定义异常
                .reader(hbBillLoanDetailFlatFileItemReader)
                .processor(hbBillLoanDetailProcessor)
                .writer(hbBillLoanDetailJpaItemWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(5) ////这些配置参数需要单独按job配置到一个公共的数据类中，支持通过application.yml 调整修改
                .build();
    }




}
